package com.team2.hospital.controller;

import com.team2.hospital.dao.OrderMapper;
import com.team2.hospital.entity.Orders;
import com.team2.hospital.entity.Patient;
import com.team2.hospital.service.DoctorService;
import com.team2.hospital.service.OrderService;
import com.team2.hospital.service.PatientService;
import com.team2.hospital.utils.JwtUtil;
import com.team2.hospital.utils.PdfUtil;
import com.team2.hospital.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("patient")
public class PatientController {

    @Autowired
    private DoctorService doctorService;
    @Autowired
    private PatientService patientService;
    @Autowired
    private OrderService orderService;

    @Resource
    private OrderMapper orderMapper;

    /**
     * 登录数据验证
     *
     * @param pId       患者ID
     * @param pPassword 患者密码
     * @return 返回登录结果及患者信息
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData login(@RequestParam(value = "pId") int pId, @RequestParam(value = "pPassword") String pPassword) {
        System.out.println("[login] 请求开始，患者ID：" + pId);
        Patient patient = this.patientService.login(pId, pPassword);
        if (patient != null) {
            Map<String, String> map = new HashMap<>();
            map.put("pName", patient.getPName());
            map.put("pId", String.valueOf(patient.getPId()));
            map.put("pCard", patient.getPCard());
            String token = JwtUtil.getToken(map);
            map.put("token", token);
            // response.setHeader("token", token); // 如果需要设置响应头，可以取消注释
            return ResponseData.success("登录成功", map);
        } else {
            return ResponseData.fail("登录失败，密码或账号错误");
        }
    }

    /**
     * 根据科室查询所有医生信息
     *
     * @param dSection 科室名称
     * @return 返回医生信息
     */
    @RequestMapping("findDoctorBySection")
    public ResponseData findDoctorBySection(@RequestParam(value = "dSection") String dSection) {
        System.out.println("[findDoctorBySection] 请求开始，科室：" + dSection);
        return ResponseData.success("根据科室查询所有医生信息成功", this.doctorService.findDoctorBySection(dSection));
    }

    /**
     * 增加挂号信息
     *
     * @param order 挂号信息
     * @param arId  排班ID
     * @return 返回挂号信息插入结果
     */
    @RequestMapping("addOrder")
    @ResponseBody
    public ResponseData addOrder(Orders order, String arId) {
        System.out.println("[addOrder] 请求开始，挂号信息：" + order.toString() + "，排班ID：" + arId);
        if (this.orderService.addOrder(order, arId)) {
            return ResponseData.success("插入挂号信息成功");
        }
        return ResponseData.fail("插入挂号信息失败");
    }

    /**
     * 根据患者ID查询挂号信息
     *
     * @param pId 患者ID
     * @return 返回患者的挂号信息
     */
    @RequestMapping("findOrderByPid")
    public ResponseData findOrderByPid(@RequestParam(value = "pId") int pId) {
        System.out.println("[findOrderByPid] 请求开始，患者ID：" + pId);
        return ResponseData.success("返回挂号信息成功", this.orderService.findOrderByPid(pId));
    }

    /**
     * 增加患者信息
     *
     * @param patient 患者信息
     * @return 返回患者注册结果
     */
    @RequestMapping("addPatient")
    @ResponseBody
    public ResponseData addPatient(Patient patient) {
        System.out.println("[addPatient] 请求开始，患者信息：" + patient.toString());
        Boolean bo = this.patientService.addPatient(patient);
        if (bo) {
            return ResponseData.success("注册成功");
        }
        return ResponseData.fail("注册失败！账号或邮箱已被占用");
    }

    /**
     * 导出挂号订单PDF
     *
     * @param request  请求
     * @param response 响应
     * @param oId      订单ID
     * @throws Exception 导出过程中可能抛出的异常
     */
    @GetMapping("/pdf")
    public void downloadPDF(HttpServletRequest request, HttpServletResponse response, int oId) throws Exception {
        System.out.println("[downloadPDF] 请求开始，订单ID：" + oId);
        Orders order = this.orderMapper.findOrderByOid(oId);
        PdfUtil.ExportPdf(request, response, order);
    }

    /**
     * 统计患者年龄及性别分布
     *
     * @return 返回统计结果
     */
    @RequestMapping("patientAge")
    public ResponseData patientAge() {
        System.out.println("[patientAge] 请求开始，统计患者年龄及性别分布");
        return ResponseData.success("统计患者男女人数成功", this.patientService.patientAge());
    }

}
